package ench.zxd;

import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.EncodingAttributes;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ddgf
 */
public class AudioConvert extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AudioConvert() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		//获取工程目录
		String realPath = this.getServletContext().getRealPath("/");
		
		// 注意：/convert_before.amr 前的“/”不能去掉，否则Linux下路径是不正确的！！
		String sourceStr = realPath + "/convert_before.amr";
		String targetStr = realPath + "/convert_after.mp3";

		File source = new File(sourceStr);
		File target = new File(targetStr);
		
		AudioAttributes audio = new AudioAttributes();
		Encoder encoder = new Encoder();
		try {
			for (String ss : encoder.getSupportedEncodingFormats()) {
				System.out.println(ss);
			}
			// pcm_s16le libmp3lame libvorbis libfaac
			audio.setCodec("libmp3lame");
			EncodingAttributes attrs = new EncodingAttributes();
			attrs.setFormat("mp3");
			attrs.setAudioAttributes(audio);
			encoder.encode(source, target, attrs);
		} catch (EncoderException e) {
			e.printStackTrace();
		}
		System.out.println("转换前音频路径："+sourceStr);
		System.out.println("转换后音频路径："+targetStr);
//		转换前音频路径：F:\project\ssb\04daqingspace\code\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\audio_convert\/convert_before.amr
//		转换后音频路径：F:\project\ssb\04daqingspace\code\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\audio_convert\/convert_after.mp3
	}
}
